package com.yuanxin.app.appjob.controller.pc;

import com.yuanxin.app.appjob.appobject.AssignmentAO;
import com.yuanxin.app.appjob.appobject.PersonInfoAO;
import com.yuanxin.app.appjob.appobject.WorkProjectAO;
import com.yuanxin.app.appjob.appobject.WorkReportAO;
import com.yuanxin.app.appjob.appobject.WorkingProgressAO;

import com.yuanxin.app.appjob.common.MVCViewName;
import com.yuanxin.app.appjob.entity.gen.AssignmentCriteria;
import com.yuanxin.app.appjob.entity.gen.PersonInfoCriteria;
import com.yuanxin.app.appjob.entity.gen.WorkProjectCriteria;
import com.yuanxin.app.appjob.entity.gen.WorkReportCriteria;
import com.yuanxin.app.appjob.entity.gen.WorkingDetailCriteria;
import com.yuanxin.app.appjob.entity.gen.WorkingProgressCriteria;
import com.yuanxin.app.appjob.service.IWorkProject;
import com.yuanxin.app.appjob.service.IWorkReportService;
import com.yuanxin.app.appjob.service.IWorkingProgressService;
import com.yuanxin.app.appjob.service.IAssignment;
import com.yuanxin.app.appjob.service.IPersonInfo;
import com.yuanxin.app.appjob.service.WorkProject;
import com.yuanxin.framework.logging.Logger;
import com.yuanxin.framework.logging.LoggerFactory;
import com.yuanxin.framework.service.ServiceResult;
import com.yuanxin.framework.util.DateTimeUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import javax.validation.Valid;
import org.springframework.ui.Model;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * 工作进展控制器。
 * 
 */
@Controller
@RequestMapping(value = "/pc/workingprogress")
public class WorkingProgressController {
	@Resource
	IWorkingProgressService workingProgressService;
	@Resource
	IWorkProject WorkProject;
	@Resource
	IWorkReportService WorkReportService;
	@Resource
	IPersonInfo personInfoService;
	
	@Resource
	IAssignment assignmentService;
	
    private static Logger LOG = LoggerFactory.getLogger(WorkingProgressController.class);

    @RequestMapping(value = "", method = { RequestMethod.GET, RequestMethod.POST })
    public ModelAndView getHello(Model model, HttpServletRequest request) {
        LOG.info("有访问来自，IP: %s USER-AGENT: %s", request.getRemoteAddr(), request.getHeader("user-agent"));
        LOG.info("SessionId %s", request.getSession().getId());
        //将当前时间传到前端
        model.addAttribute("currentTime", DateTimeUtils.formateDateToStr(new Date(),DateTimeUtils.FORMAT_YMD_HMS));
		//将当前运用名称传到前端
        model.addAttribute("currentUser",request.getSession());//把用户信息传到前端
        
        ModelAndView mv = new ModelAndView();
        mv.addObject(model);
        
        return mv;
    }
    

    
    @RequestMapping(value = "/list", method = { RequestMethod.GET, RequestMethod.POST })
    public ModelAndView list(Model model, HttpServletRequest request) {
        LOG.info("有访问来自，IP: %s USER-AGENT: %s", request.getRemoteAddr(), request.getHeader("user-agent"));
        LOG.info("SessionId %s", request.getSession().getId());
        //将当前时间传到前端
        model.addAttribute("currentTime", DateTimeUtils.formateDateToStr(new Date(),DateTimeUtils.FORMAT_YMD_HMS));
		//将当前运用名称传到前端
        model.addAttribute("currentUser",request.getSession());//把用户信息传到前端
        
        ModelAndView mv = new ModelAndView();
        WorkProjectCriteria cc = new WorkProjectCriteria();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");       
        Date date = new Date();       
        String formatDate = sdf.format(date);           //获取当前的年份
        Calendar cal = Calendar.getInstance(); 
    	cal.setFirstDayOfWeek(Calendar.SUNDAY); 
        //取当前日期的年份里面的周数 
        int currentWeekOfYear = cal.get(Calendar.WEEK_OF_YEAR); 
        String s =  String.valueOf(currentWeekOfYear);
        
        PersonInfoAO pf = (PersonInfoAO)request.getSession().getAttribute("currentUser");
//        PersonInfoCriteria pc = new PersonInfoCriteria();
//        pc.createCriteria().andIdEqualTo(pf.getId());
//        ServiceResult<List<PersonInfoAO>> plist = personInfoService.selectByCriteria(pc);
//        PersonInfoAO upf = plist.getData().get(0);//取出用户信息
//        PersonInfoAO upf = personInfoService.getById(new Integer(pf.getId())).get;//取出用户信息
        
        List<Long> joblist = new ArrayList<Long>();
        AssignmentCriteria ac = new  AssignmentCriteria();
        ac.createCriteria().andPersonIdEqualTo(pf.getId());
        ServiceResult<List<AssignmentAO>> alist =  assignmentService.selectByCriteria(ac);//取出Assignment与personid相同的信息
        int datasize =alist.getData().size();
        for(int i=0;i<datasize;i++){
        	AssignmentAO aa = alist.getData().get(i);
        	long b = Long.parseLong(aa.getJobId());
        	joblist.add(b);
         }
        if(joblist.size() == 0){//关于joblist为空时的判断
        	model.addAttribute("datalist", new ArrayList());
        	
        }else{
        	cc.createCriteria().andWeekNumEqualTo(s).andCurrentYearEqualTo(formatDate).andJobIdIn(joblist);    //formatDate表示当前时间所在的年数  前端只显示本年的数据；
 	           ServiceResult<List< WorkProjectAO>> list =  WorkProject.selectByCriteria(cc);
 	        if(list!=null&& list.getData()!=null&& list.getData().size()>0){
	        	model.addAttribute("datalist", list.getData());
	        }else{
	        	model.addAttribute("datalist", new ArrayList());
	        }
	       
        }
        
       
        mv.addObject(model);
        mv.setViewName(MVCViewName.APP_APPJOB_PC_WORKPROJECT_WORKPROJECTLIST.toString());
        return mv;
        
     }
    
   
    
    
    @RequestMapping(value = "/showform/{id}", method = { RequestMethod.GET, RequestMethod.POST })
    public ModelAndView showform(@PathVariable String id , Model model, HttpServletRequest request) {
    	LOG.info("有访问来自，IP: %s USER-AGENT: %s", request.getRemoteAddr(), request.getHeader("user-agent"));
    	LOG.info("SessionId %s", request.getSession().getId());
    	//将当前时间传到前端
    	model.addAttribute("currentTime", DateTimeUtils.formateDateToStr(new Date(),DateTimeUtils.FORMAT_YMD_HMS));
    	//将当前运用名称传到前端
    	model.addAttribute("currentUser",request.getSession());//把用户信息传到前端
    	
    	
    	 SimpleDateFormat sdf = new SimpleDateFormat("yyyy");       
         Date date = new Date();       
         String formatDate = sdf.format(date);           //获取当前的年份
         Calendar cal = Calendar.getInstance(); 
     	cal.setFirstDayOfWeek(Calendar.SUNDAY); 
         //取当前日期的年份里面的周数 
         int currentWeekOfYear = cal.get(Calendar.WEEK_OF_YEAR); 
         
         String m =  String.valueOf(currentWeekOfYear-1);
         
        WorkProjectCriteria wc =new WorkProjectCriteria();
         long t = Long.parseLong(id);
     	 wc.createCriteria().andProgressIdEqualTo(t);
     	 ServiceResult<List<WorkProjectAO>> plist = WorkProject.selectByCriteria(wc);
      	WorkProjectCriteria cc = new WorkProjectCriteria();
     	WorkProjectAO wa =plist.getData().get(0);
     	 
     	 cc.createCriteria().andWeekNumEqualTo(m).andCurrentYearEqualTo(formatDate).andJobIdEqualTo(wa.getJobId());
     	 ServiceResult<List<WorkProjectAO>> list = WorkProject.selectByCriteria(cc);
     	 
    	ServiceResult<WorkingProgressAO> dataAO = workingProgressService.getById(id);   	
    	model.addAttribute("dataAO", dataAO.getData());   //m表示上一周的周数
     	 /*WorkProjectCriteria cc = new WorkProjectCriteria(); 
     	 
     	PersonInfoAO pf = (PersonInfoAO)request.getSession().getAttribute("currentUser");
       PersonInfoCriteria pc = new PersonInfoCriteria();
         pc.createCriteria().andIdEqualTo(pf.getId());
        ServiceResult<List<PersonInfoAO>> plist = personInfoService.selectByCriteria(pc);
        PersonInfoAO upf = plist.getData().get(0);//取出用户信息
        
        List<Long> joblist = new ArrayList<Long>();
        AssignmentCriteria ac = new  AssignmentCriteria();
        ac.createCriteria().andPersonIdEqualTo(upf.getId());
        ServiceResult<List<AssignmentAO>> alist =  assignmentService.selectByCriteria(ac);//取出Assignment与personid相同的信息
        int datasize =alist.getData().size();
        for(int i=0;i<datasize;i++){
        	AssignmentAO aa = alist.getData().get(i);
        	long b = Long.parseLong(aa.getJobId());
        	joblist.add(b);
         }
        
       cc.createCriteria().andWeekNumEqualTo(m).andCurrentYearEqualTo(formatDate).andJobIdIn(joblist);    //formatDate表示当前时间所在的年数  前端只显示本年的数据；
     
     	 
     	ServiceResult<List<WorkProjectAO>> list = WorkProject.selectByCriteria(cc); */
         if(list!=null&& list.getData()!=null&& list.getData().size()>0){
         	model.addAttribute("datalistsk", list.getData());
         }else{
           model.addAttribute("datalistsk", new ArrayList());
         }
    	ModelAndView mv = new ModelAndView();
    	mv.addObject(model);
    	mv.setViewName(MVCViewName.APP_APPJOB_PC_WORKPROGRESS_WORKPROGRESSFORM.toString());
    	return mv;
    }  
    
    @RequestMapping(value = "/view/{dataId}", method = { RequestMethod.GET, RequestMethod.POST })
    public ModelAndView viewById(@PathVariable String dataId,Model model, HttpServletRequest request) {
        LOG.info("有访问来自，IP: %s USER-AGENT: %s", request.getRemoteAddr(), request.getHeader("user-agent"));
        LOG.info("SessionId %s", request.getSession().getId());
        //将当前时间传到前端
        model.addAttribute("currentTime", DateTimeUtils.formateDateToStr(new Date(),DateTimeUtils.FORMAT_YMD_HMS));
		//将当前运用名称传到前端
        model.addAttribute("currentUser",request.getSession());//把用户信息传到前端
        
        ModelAndView mv = new ModelAndView();
        mv.addObject(model);
        
        return mv;
    }
    
    
    @RequestMapping(value = "/update/{dataId}", method = { RequestMethod.GET, RequestMethod.POST })
    public ModelAndView updateById(@PathVariable String dataId,Model model, HttpServletRequest request) {
        LOG.info("有访问来自，IP: %s USER-AGENT: %s", request.getRemoteAddr(), request.getHeader("user-agent"));
        LOG.info("SessionId %s", request.getSession().getId());
        //将当前时间传到前端
        model.addAttribute("currentTime", DateTimeUtils.formateDateToStr(new Date(),DateTimeUtils.FORMAT_YMD_HMS));
		//将当前运用名称传到前端
        model.addAttribute("currentUser",request.getSession());//把用户信息传到前端
        
        ModelAndView mv = new ModelAndView();
        mv.addObject(model);
        
        return mv;
    }
    
    @RequestMapping(value = "/delete/{dataId}", method = { RequestMethod.GET, RequestMethod.POST })
    @ResponseBody
    public Object deleteById(@PathVariable String dataId, HttpServletRequest request) {
        LOG.info("有访问来自，IP: %s USER-AGENT: %s", request.getRemoteAddr(), request.getHeader("user-agent"));
        LOG.info("SessionId %s", request.getSession().getId());
        //将当前时间传到前端
		//将当前运用名称传到前端
        ServiceResult<String> result = new ServiceResult<String>();
        boolean flag = false;
        try {
        	WorkingProgressAO aa = new WorkingProgressAO();
        	aa.setId(dataId);
        	aa.setStatusDel("1");
        	workingProgressService.saveOrUpdate(aa);
        	 result.setSucceed(flag);
 			 result.setMsg("删除成功！");
        } catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			result.setSucceed(flag);
			result.setMsg("删除失败！");
		}
        return result;
    }
    
    /**
    *异步提交表单
    **/
    @RequestMapping(value = "/submitAJAXForm", method = { RequestMethod.GET, RequestMethod.POST })
    @ResponseBody
    public Object submitAJAXForm(@Valid WorkingProgressAO source,Model model, HttpServletRequest request) {
        LOG.info("有访问来自，IP: %s USER-AGENT: %s", request.getRemoteAddr(), request.getHeader("user-agent"));
        LOG.info("SessionId %s", request.getSession().getId());
        //将当前时间传到前端
        model.addAttribute("currentTime", DateTimeUtils.formateDateToStr(new Date(),DateTimeUtils.FORMAT_YMD_HMS));
		ServiceResult<Object> ret = new ServiceResult<Object>();

        return ret;
    }

   @RequestMapping(value = "/submitForm", method = { RequestMethod.GET, RequestMethod.POST })
   public String submitForm(@Valid WorkingProgressAO source,Model model, HttpServletRequest request) {
   	LOG.info("有访问来自，IP: %s USER-AGENT: %s", request.getRemoteAddr(), request.getHeader("user-agent"));
   	LOG.info("SessionId %s", request.getSession().getId());
   	//将当前时间传到前端
   	model.addAttribute("currentTime", DateTimeUtils.formateDateToStr(new Date(),DateTimeUtils.FORMAT_YMD_HMS));
   	model.addAttribute("currentUser",request.getSession());//把用户信息传到前端
   	
   	WorkingProgressAO aa=source;
	
 
    workingProgressService.saveOrUpdate(aa);
   	
   	return "redirect:/pc/workingprogress/list";
   }
   
   
	@RequestMapping(value = "/setstatus/{status}/{dataId}", method = { RequestMethod.GET, RequestMethod.POST })
    @ResponseBody
    public Object deleteById(@PathVariable String status,@PathVariable String dataId, HttpServletRequest request) {
        LOG.info("有访问来自，IP: %s USER-AGENT: %s", request.getRemoteAddr(), request.getHeader("user-agent"));
        LOG.info("SessionId %s", request.getSession().getId());
        //将当前时间传到前端
		//将当前运用名称传到前端
        ServiceResult<String> result = new ServiceResult<String>();
        boolean flag = false;
        try {
        	WorkingProgressAO aa=new WorkingProgressAO();
        	aa.setId(dataId);
        	aa.setStatus(status);        	
        	workingProgressService.saveOrUpdateRetAO(aa);
        	 result.setSucceed(flag);
 			 result.setMsg("设置成功！");
        } catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			result.setSucceed(flag);
			result.setMsg("设置失败！");
		}
        return result;
    }
}

